package com.example.app2024.mnote;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.app2024.R;

public class MnoteActivity extends AppCompatActivity implements View.OnClickListener
{
      private Button textbtn, imgbtn, videobtn;
      private ListView lv;
      private Intent i;
      private MyAdapter adapter;
      private NotesDB notesDB;
      private SQLiteDatabase dbReader;
      private Cursor cursor;

      @Override
      protected void onCreate(Bundle savedInstanceState)
      {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_mnote);
            initView();
      }

      public void initView()
      {
            lv = (ListView) findViewById(R.id.list);
            textbtn = (Button) findViewById(R.id.text);
            imgbtn = (Button) findViewById(R.id.img);
            videobtn = (Button) findViewById(R.id.video);
            textbtn.setOnClickListener(this);
            imgbtn.setOnClickListener(this);
            videobtn.setOnClickListener(this);
            notesDB = new NotesDB(this);
            dbReader = notesDB.getReadableDatabase();

            lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
            {
                  @Override
                  public void onItemClick(AdapterView<?> parent, View view, int position, long id)
                  {
                        cursor.moveToPosition(position);
                        Intent i = new Intent(MnoteActivity.this, SelectAct.class);
                        i.putExtra(NotesDB.ID, cursor.getInt((int)cursor.getColumnIndex(NotesDB.ID)));
                        i.putExtra(NotesDB.CONTENT, cursor.getString((int)cursor.getColumnIndex(NotesDB.CONTENT)));
                        i.putExtra(NotesDB.TIME, cursor.getString((int)cursor.getColumnIndex(NotesDB.TIME)));
                        i.putExtra(NotesDB.PATH, cursor.getString((int)cursor.getColumnIndex(NotesDB.PATH)));
                        i.putExtra(NotesDB.VIDEO, cursor.getString((int)cursor.getColumnIndex(NotesDB.VIDEO)));
                        startActivity(i);
                  }
            });
      }

      @Override
      public void onClick(View v)
      {
            i = new Intent(this, AddContent.class);

            int id = v.getId();
            if (id == R.id.text)
            {
                  i.putExtra("flag", "1");
                  startActivity(i);
            }
            else if (id == R.id.img)
            {
                  i.putExtra("flag", "2");
                  startActivity(i);
            }
            else if (id == R.id.video)
            {
                  i.putExtra("flag", "3");
                  startActivity(i);
            }
      }

      public void selectDB()
      {
            cursor = dbReader.query(NotesDB.TABLE_NAME, null, null, null, null, null, null);
            adapter = new MyAdapter(this, cursor);
            lv.setAdapter(adapter);
      }

      @Override
      protected void onResume()
      {
            super.onResume();
            selectDB();
      }
}